//
//  LoginViewController.m
//  haolaDoc
//
//  Created by WayneHao on 2017/5/16.
//  Copyright © 2017年 deyi. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _loginScrollView.contentSize=CGSizeMake(0, self.view.frame.size.height);
    self.isRemeber=false;
}

-(void)viewWillAppear:(BOOL)animated{
   
    self.loginButton.layer.cornerRadius=5;
    self.registerButton.layer.cornerRadius=5;
    self.usernameView.layer.cornerRadius=10;
    self.passwordView.layer.cornerRadius=10;
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isRememberOption = [accountDefaults objectForKey:@"isRemember"];
    if ([isRememberOption isEqualToString:@"YES"]) {
        self.usernameField.text = [accountDefaults objectForKey:@"username"];
        self.passwordField.text = [accountDefaults objectForKey:@"password"];
    }else{
        self.usernameField.text = [accountDefaults objectForKey:@"username"];
    }
}
- (IBAction)onLoginAction:(id)sender {
    NSString *username=self.usernameField.text;
    NSString *password=self.passwordField.text;
    if ([AppMonitor isConnectionAvailable]) {
        [self doLoginAction:username andPassword:password];
    }else{
        [SVProgressHUD showWithStatus:@"无网络连接"];
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.7];
    }

}
-(void)dismiss{
    [SVProgressHUD dismiss];
}

-(void)doLoginAction:(NSString *)username andPassword:(NSString *)password{
    NSString *urlStr=[NSString stringWithFormat:BASE_URL@"/doctorinfo/login/%@-%@",username,password];
    NSURL *url=[NSURL URLWithString:urlStr];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSError *error;
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    NSDictionary *content = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];//转换数据格式
    NSString *status= [content objectForKey:@"status"];
    NSString *msg = [content objectForKey:@"msg"];
//    NSString *NIMToken =[content objectForKey:@"NIMToken"];
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    if ([status isEqual:@"success"]) {
        if(self.isRemeber){ //记住密码后将用户名和密码数据保存到userDefaultData中，注销后仍然保存，在下次不保存密码的登录注销后，数据将被销毁
            [accountDefaults setObject:msg forKey:@"userId"];
            [accountDefaults setObject:username forKey:@"username"];
            [accountDefaults setObject:password forKey:@"password"];
            [accountDefaults setObject:@"YES" forKey:@"isRemember"];
            [accountDefaults setObject:@"online" forKey:@"isOnline"];
        }else{//否则将仅保存userId
            [accountDefaults setObject:msg forKey:@"userId"];
            [accountDefaults setObject:username forKey:@"username"];
            [accountDefaults setObject:password forKey:@"password"];
            [accountDefaults setObject:@"NO" forKey:@"isRemember"];
            [accountDefaults setObject:@"online" forKey:@"isOnline"];
        }
        [[self navigationController] popViewControllerAnimated:YES];
        //        [self showResult:@"登录成功"];
//        NSString *nimUsername = [NSString stringWithFormat:@"doctor%@",username];
//        [[[NIMSDK sharedSDK] loginManager] login:nimUsername token:NIMToken completion:^(NSError * _Nullable error) {
//            NSLog(@"login yx %@",error);
//        }];
    }else{
        [SVProgressHUD showWithStatus:@"登录失败，请重试"];
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.7];
    }

}
- (IBAction)onRememberme:(id)sender {
    self.isRemeber=!self.isRemeber;
    if (self.isRemeber) {
        [self.remeberMe setImage:[UIImage imageNamed:@"check_selected.png"] forState:UIControlStateNormal];
    }else{
        [self.remeberMe setImage:[UIImage imageNamed:@"check_unselected.png"] forState:UIControlStateNormal];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
